package com.example.com.skills_utf8;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

public class TTActivity extends Activity{

	TTActivity activity = this;
	ListView lv;
	
	public ArrayList<String> data = new ArrayList<String>();
	
	TTAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_tt);
		
		
		
		data.add("1");
		data.add("2");
		data.add("3");
		adapter = new TTAdapter(activity);
		
		
		lv =(ListView)findViewById(R.id.lv);
		lv.setAdapter(adapter);
		
		
		/*
		 lv.setOnTouchListener( new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					
				
				
					
					
					return false;
				}
			});
		 */
		 
	      lv.setOnItemClickListener( new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent,
						View view, int position, long id) {
					
					View layout_view = view.findViewById(R.id.layout_inside1);
					final View layout_view_delete = view.findViewById(R.id.layout_inside2);
					
					Rect outRect = new Rect();
					view.getHitRect(outRect);
					System.out.println("hit: "+outRect);
					
					/*
					03-12 17:44:01.683: I/System.out(29105): hit: Rect(0, 0 - 720, 64)
					03-12 17:44:01.683: I/System.out(29105): hit1: Rect(720, 0 - 720, 64)
					Rect outRect1 = new Rect();
					layout_view_delete.getHitRect(outRect1);
					System.out.println("hit1: "+outRect1);
					*/
					
					
					System.out.println("onclick: "+view);
					System.out.println("onclick: "+(String)view.getTag());
					{
						int fromXDelta = layout_view.getLeft();
						int toXDelta = -200;
						TranslateAnimation ta = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
						layout_view.startAnimation(ta);
						ta.setFillAfter(true);
					}
					
					{
						int width = 200; //view.getWidth()
						int fromXDelta = layout_view_delete.getLeft();
						int toXDelta = fromXDelta - width;
						TranslateAnimation ta = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
						layout_view_delete.startAnimation(ta);
						ta.setFillAfter(true);
						
						final LinearLayout.LayoutParams contentParams = 
								(LinearLayout.LayoutParams)layout_view_delete.getLayoutParams();
						
						// 设置动画监听器	
						ta.setAnimationListener(new AnimationListener()  
						{
							public void onAnimationEnd(Animation animation) {		// Make movement of content permanent after animation has completed 
								contentParams.setMargins(-200, 0, 0, 0);			// by positioning its left margin; margin是控件边缘相对父控件，或者其他控件的边距．
								layout_view_delete.setLayoutParams(contentParams); 
							}

							public void onAnimationRepeat(Animation animation) { }
							public void onAnimationStart(Animation animation) { }
						});
						
						
					}
					
					
					
					
					
				}
	     	 
			});
	      
	      
	}
	
	
	
	
	
	
	void trans(View last, View now){
		
		float fromXDelta = last.getLeft();
		float toXDelta = now.getLeft();
		System.out.println(""+fromXDelta);
		System.out.println(""+toXDelta);
		
		/*
		TranslateAnimation ta = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
	//	ta.setDuration(200);
		ta.setFillAfter(true);
		ImageView bottom_triangle = (ImageView)findViewById(R.id.bottom_triangle);
		bottom_triangle.startAnimation(ta);
		*/
	}
	
	
	
	
}




class TTAdapter extends BaseAdapter{

	TTActivity activity;
	LayoutInflater inflater;
	public TTAdapter(TTActivity activity) {
		// TODO Auto-generated constructor stub
		
		this.activity = activity;
		inflater =(LayoutInflater) activity.getSystemService( activity.LAYOUT_INFLATER_SERVICE);
		
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return activity.data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return activity.data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		System.out.println("adapter getView.");
		
		if(convertView == null)
		convertView =inflater.inflate(R.layout.activity_tt_item, parent, false);
		
		
		
		
		
		return convertView;
	}
	
	
	
}







